using System;
using System.Data;
using System.Collections;

using EntLibEC.Dac;
using System.Transactions;
using EntLibEC.Dac.Online;
using EntLibEC.Info.Online;
using EntLibEC.Cmn;
namespace EntLibEC.Biz.Online
{
	/// <summary>
	/// Summary description for AdvQuestionManager.
	/// </summary>
	public class AdvQuestionManager
	{
		private AdvQuestionManager()
		{		   
		}
		private static AdvQuestionManager _instance;
		public static AdvQuestionManager GetInstance()
		{
			if( _instance == null )
			{
				_instance = new AdvQuestionManager();
			}
			return _instance;
		}

		public void InsertAdvQuestion(AdvQuestionInfo oInfo)
		{
		    new AdvQuestionDac().Insert(oInfo);
		}

		public void InsertAdvQuestionItem(AdvQuestionItemInfo oInfo)
		{
		    new AdvQuestionItemDac().Insert(oInfo);
		}

		public DataSet GetAdvQuestionList()
		{
		    string sql = @"	select Ent_Adv_Question.* , a.customer_qty from Ent_Adv_Question

							left join
							(select Ent_Adv_Question.sysno , count(Ent_Adv_Question_customer.sysno) as customer_qty

							from Ent_Adv_Question

							inner join Ent_Adv_Question_customer on Ent_Adv_Question.sysno = Ent_Adv_Question_customer.activitysysno

							group by Ent_Adv_Question.sysno) as a
							on a.sysno = Ent_Adv_Question.sysno";
			return SqlHelper.ExecuteDataSet(sql);
		}

		public void Delete(int sysno)
		{
			TransactionOptions options = new TransactionOptions();
            options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            options.Timeout = TransactionManager.DefaultTimeout;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
            {

				new AdvQuestionDac().Delete(sysno);
				new AdvQuestionItemDac().DeleteByActivitySysNo(sysno);

				scope.Complete();
            }
		}
		public void DeleteAdvQuestionItem(int sysno)
		{
		    new AdvQuestionItemDac().Delete(sysno);
		}

		public DataSet GetQuestionItemBySysNo(int sysno)
		{
		    string sql="select * from Ent_Adv_Question_item where activitysysno=" + sysno ;
			DataSet ds = SqlHelper.ExecuteDataSet(sql);
			return ds;
		}

		public DataSet LoadAdvQuestion(int sysno)
		{
		    string sql = @"select Ent_Adv_Question.* , Ent_Sys_User.username as createusername
                           from Ent_Adv_Question 
                           inner join Ent_Sys_User on Ent_Adv_Question.createusersysno = Ent_Sys_User.sysno 
                           where Ent_Adv_Question.sysno = " + sysno ;
			DataSet ds = SqlHelper.ExecuteDataSet(sql);
			return ds;          
		}

		public void InsertAdvQuestionCustomer(AdvQuestionCustomerInfo oInfo)
		{
		    string sql = "select * from Ent_Adv_Question_Customer where customersysno=" + oInfo.CustomerSysNo + " and activitysysno =" + oInfo.ActivitySysNo;
			DataSet ds = SqlHelper.ExecuteDataSet(sql);
			if(Util.HasMoreRow(ds))
				return;

			new AdvQuestionCustomerDac().Insert(oInfo);
		}
		
	}
}
